﻿using System.Data;
using System.IO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html.simpleparser;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

namespace AccessWare
{
    public partial class Report : System.Web.UI.Page
    {
        DataSet ds = new DataSet();
        protected void Page_Load(object sender, EventArgs e)
        {
            string strSPNAme = "getreport";
            SPCalling objSPCalling = new SPCalling();
             ds = objSPCalling.ExecuteDataset(CommandType.StoredProcedure, strSPNAme);
            gdReportList.DataSource = ds.Tables[0];
            gdReportList.DataBind();
            gdReportList.Visible = true;
        }



        protected void EXOIRTTOeXCEL(object sender, EventArgs e)
        {
           


            GridView GridView1 = new GridView();

            GridView1.AllowPaging = false;

            GridView1.DataSource = ds.Tables[0];

            GridView1.DataBind();

            //Change the Header Row Foreground to white color
            GridView1.HeaderRow.Style.Add("color", "#FFFFFF");
            //Applying stlye to gridview header cells
            for (int i = 0; i < GridView1.HeaderRow.Cells.Count; i++)
            {
                //Change to Sea green
                GridView1.HeaderRow.Cells[i].Style.Add("background-color", "#009F82");
            }

            Response.Clear();

            Response.Buffer = true;

            Response.AddHeader("content-disposition",

             "attachment;filename=DataTable.xls");

            Response.Charset = "";

            Response.ContentType = "application/vnd.ms-excel";

            StringWriter sw = new StringWriter();

            HtmlTextWriter hw = new HtmlTextWriter(sw);



            for (int i = 0; i < GridView1.Rows.Count; i++)
            {

                //Apply text style to each Row

                GridView1.Rows[i].Attributes.Add("class", "textmode");

            }

            GridView1.RenderControl(hw);



            //style to format numbers to string

            string style = @"<style> .textmode { mso-number-format:\@; } </style>";

            Response.Write(style);

            Response.Output.Write(sw.ToString());

            Response.Flush();

            Response.End();
        }


        protected void ExportToPDF(object sender, EventArgs e)
        {
            

            GridView GridView1 = new GridView();

            GridView1.AllowPaging = false;

            GridView1.DataSource = ds.Tables[0];

            GridView1.DataBind();

            //GridView1.HeaderRow.Style.Add("color", "#FFFFFF");
            //Applying stlye to gridview header cells
            for (int i = 0; i < GridView1.HeaderRow.Cells.Count; i++)
            {
                //Change to Sea green
                GridView1.HeaderRow.Cells[i].Style.Add("background-color", "#009F82");
            }

            Response.ContentType = "application/pdf";

            Response.AddHeader("content-disposition",

                "attachment;filename=DataTable.pdf");

            Response.Cache.SetCacheability(HttpCacheability.NoCache);

            StringWriter sw = new StringWriter();

            HtmlTextWriter hw = new HtmlTextWriter(sw);

            GridView1.RenderControl(hw);

            StringReader sr = new StringReader(sw.ToString());
            Document pdfDoc = new Document(PageSize.A0, 10f, 10f, 10f, 0f);

            HTMLWorker htmlparser = new HTMLWorker(pdfDoc);

            PdfWriter.GetInstance(pdfDoc, Response.OutputStream);

            pdfDoc.Open();

            htmlparser.Parse(sr);

            pdfDoc.Close();

            Response.Write(pdfDoc);

            Response.End();

        }
    }
}